﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace SAD_Assignment1.DAO
{
    public class LikeDAO
    {
        public int countLikePhoto(int photoID, bool isLike)
        {
            int result = 0;
            try
            {
                string connStr = ConfigurationManager.ConnectionStrings["DBSource"].ConnectionString;

                using (var conn = new SqlConnection(connStr))
                using (var cmd = conn.CreateCommand())
                {
                    conn.Open();
                    cmd.CommandText = "SELECT dbo.countLikePhoto(@photoID,@isLike)";
                    cmd.Parameters.AddWithValue("@photoID", photoID);
                    cmd.Parameters.AddWithValue("@isLike", isLike);
                    result = (int)cmd.ExecuteScalar();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return result;
        }

        public bool LikePhoto(int photoID, int accID, bool isLike)
        {
            bool result = false;
            try
            {
                string connStr = ConfigurationManager.ConnectionStrings["DBSource"].ConnectionString;

                using (var conn = new SqlConnection(connStr))
                using (var cmd = conn.CreateCommand())
                {
                    conn.Open();
                    cmd.CommandText = "LikePhoto";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@photoID", photoID);
                    cmd.Parameters.AddWithValue("@accID", accID);
                    cmd.Parameters.AddWithValue("@isLike", isLike);
                    SqlParameter res = new SqlParameter();
                    res.ParameterName = "@result";
                    res.SqlDbType = System.Data.SqlDbType.Bit;
                    res.Direction = System.Data.ParameterDirection.Output;
                    cmd.Parameters.Add(res);
                    cmd.ExecuteNonQuery();
                    result = (bool)res.Value;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return result;
        }

        public bool IsLikePhoto(int photoID, int accID, bool isLike)
        {
            bool result = false;
            try
            {
                string connStr = ConfigurationManager.ConnectionStrings["DBSource"].ConnectionString;

                using (var conn = new SqlConnection(connStr))
                using (var cmd = conn.CreateCommand())
                {
                    conn.Open();
                    cmd.CommandText = "SELECT dbo.IsLikePhoto(@photoID,@accID,@isLike)";
                    cmd.Parameters.AddWithValue("@photoID", photoID);
                    cmd.Parameters.AddWithValue("@accID", accID);
                    cmd.Parameters.AddWithValue("@isLike", isLike);
                    result = (bool)cmd.ExecuteScalar();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return result;
        }        

    }
}